<html lang="zh-CN"><head><meta charset="UTF-8"><style>.nodata  main {width:1000px;margin: auto;}</style></head><body class="nodata " style=""><div class="main_father clearfix d-flex justify-content-center " style="height:100%;"> <div class="container clearfix " id="mainBox"><main><div class="blog-content-box">
<div class="article-header-box">
<div class="article-header">
<div class="article-title-box">
<h1 class="title-article" id="articleContentId">(B卷,100分)- 报文回路（Java & JS & Python）</h1>
</div>
</div>
</div>
<div id="blogHuaweiyunAdvert"></div>

        <div id="article_content" class="article_content clearfix">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-044f2cf1dc.css">
                <div id="content_views" class="htmledit_views">
                    <h4 id="main-toc">题目描述</h4> 
<p>IGMP 协议中响应报文和查询报文&#xff0c;是维系组播通路的两个重要报文&#xff0c;在一条已经建立的组播通路中两个相邻的 HOST 和 ROUTER&#xff0c;ROUTER 会给 HOST 发送查询报文&#xff0c;HOST 收到查询报文后给 ROUTER 回复一个响应报文&#xff0c;以维持相之间的关系&#xff0c;一旦这关系断裂&#xff0c;那么这条组播通路就异常”了。现通过某种手段&#xff0c;抓取到了 HOST 和 ROUTER 两者通讯的所有响应报文和查询报文&#xff0c;请分析该组播通路是否“正常”</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%85%A5%E6%8F%8F%E8%BF%B0">输入描述</h4> 
<p>第一行抓到的报文数量C (C≤100) &#xff0c;后续C行依次输入设备节点D1和D2&#xff0c;表示从D1到D2发送了单向的报文&#xff0c;D1和D2用空格隔开。</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%87%BA%E6%8F%8F%E8%BF%B0">输出描述</h4> 
<p>组播通路是否“正常”&#xff0c;正常输出True&#xff0c; 异常输出False。</p> 
<p></p> 
<h4 id="%E7%94%A8%E4%BE%8B">用例</h4> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;">5<br /> 1 2<br /> 2 3<br /> 3 2<br /> 1 2<br /> 2 1</td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">True</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;">无</td></tr></tbody></table> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;">3<br /> 1 3<br /> 3 2<br /> 2 3</td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">False</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;">无</td></tr></tbody></table> 
<h4 id="%E9%A2%98%E7%9B%AE%E8%A7%A3%E6%9E%90">题目解析</h4> 
<p>用例1图示</p> 
<p><img alt="" height="93" src="https://img-blog.csdnimg.cn/098e91c3c37c46e2a4ad6d8d86c72909.png" width="308" /></p> 
<p></p> 
<p>用例2图示</p> 
<p><img alt="" height="82" src="https://img-blog.csdnimg.cn/43b724f1ac034bfd9e4aee8e995b866e.png" width="278" /></p> 
<p></p> 
<p>根据题目意思&#xff0c;想要组播通路之间正常&#xff0c;则有数据传递的两个节点之间必须是双向的。</p> 
<p></p> 
<p>即如果存在D1 → D2&#xff0c;那么需要检查有没有D2 → D1&#xff0c;如果没有的话&#xff0c;则通路失败。</p> 
<p>如果所有的节点和其交互的节点都存在双向传递的话&#xff0c;则整个通路成功。 </p> 
<p></p> 
<h4 id="%E7%AE%97%E6%B3%95%E6%BA%90%E7%A0%81">Java算法源码</h4> 
<pre><code class="language-java">import java.util.HashMap;
import java.util.HashSet;
import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner sc &#61; new Scanner(System.in);

    int n &#61; sc.nextInt();

    HashMap&lt;Integer, HashSet&lt;Integer&gt;&gt; trans &#61; new HashMap&lt;&gt;();
    for (int i &#61; 0; i &lt; n; i&#43;&#43;) {
      int send &#61; sc.nextInt();
      int receive &#61; sc.nextInt();
      trans.putIfAbsent(send, new HashSet&lt;&gt;());
      trans.putIfAbsent(receive, new HashSet&lt;&gt;());
      trans.get(send).add(receive);
    }

    System.out.println(getResult(trans));
  }

  public static String getResult(HashMap&lt;Integer, HashSet&lt;Integer&gt;&gt; trans) {
    for (Integer send : trans.keySet()) {
      for (Integer receive : trans.get(send)) {
        if (!trans.get(receive).contains(send)) {
          return &#34;False&#34;;
        }
      }
    }
    return &#34;True&#34;;
  }
}
</code></pre> 
<h4>JS算法源码</h4> 
<pre><code class="language-javascript">/* JavaScript Node ACM模式 控制台输入获取 */
const readline &#61; require(&#34;readline&#34;);

const rl &#61; readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

const lines &#61; [];
let n;
rl.on(&#34;line&#34;, (line) &#61;&gt; {
  lines.push(line);

  if (lines.length &#61;&#61;&#61; 1) {
    n &#61; lines[0] - 0;
  }

  if (n &amp;&amp; lines.length &#61;&#61; n &#43; 1) {
    lines.shift();

    const trans &#61; {};

    lines
      .map((line) &#61;&gt; line.split(&#34; &#34;))
      .forEach((arr) &#61;&gt; {
        const [send, receive] &#61; arr;
        if (!trans[send]) trans[send] &#61; new Set();
        if (!trans[receive]) trans[receive] &#61; new Set();
        trans[send].add(receive);
      });

    console.log(getResult(trans));

    lines.length &#61; 0;
  }
});

function getResult(trans) {
  for (let send in trans) {
    for (let receive of trans[send]) {
      if (!trans[receive].has(send)) return &#34;False&#34;;
    }
  }

  return &#34;True&#34;;
}
</code></pre> 
<h4>Python算法源码</h4> 
<pre><code class="language-python"># 输入获取
n &#61; int(input())
arr &#61; [input().split() for _ in range(n)]

trans &#61; {}
for send, receive in arr:
    if trans.get(send) is None:
        trans[send] &#61; set()

    if trans.get(receive) is None:
        trans[receive] &#61; set()

    trans[send].add(receive)


# 算法入口
def getResult():
    for s in trans:
        for c in trans[s]:
            if s not in trans[c]:
                return &#34;False&#34;

    return &#34;True&#34;


# 算法调用
print(getResult())
</code></pre>
                </div>
        </div>
        <div id="treeSkill"></div>
        <div id="blogExtensionBox" style="width:400px;margin:auto;margin-top:12px" class="blog-extension-box"></div>
    <script>
  $(function() {
    setTimeout(function () {
      var mathcodeList = document.querySelectorAll('.htmledit_views img.mathcode');
      if (mathcodeList.length > 0) {
        for (let i = 0; i < mathcodeList.length; i++) {
          if (mathcodeList[i].naturalWidth === 0 || mathcodeList[i].naturalHeight === 0) {
            var alt = mathcodeList[i].alt;
            alt = '\\(' + alt + '\\)';
            var curSpan = $('<span class="img-codecogs"></span>');
            curSpan.text(alt);
            $(mathcodeList[i]).before(curSpan);
            $(mathcodeList[i]).remove();
          }
        }
        MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
      }
    }, 1000)
  });
</script>
</div></html>